package com.work.csms.controller;

import com.work.csms.dao.Station;
import com.work.csms.pojo.PageParam;
import com.work.csms.service.StationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/station")
@CrossOrigin(originPatterns = "*",allowCredentials ="true")
public class StationController {

    @Autowired(required = false)
    private StationService stationService;

    @PostMapping("/page")
    public Map<String, Object> page(@RequestBody Station station, @RequestBody PageParam pageParam) throws Exception {
        return stationService.page(station, pageParam);
    }

    @PostMapping("/list")
    public List<Station> list(@RequestBody Station station) throws Exception {
        return stationService.list(station);
    }

    @RequestMapping("/getStationById")
    public Station getStationById(@RequestBody Station station) throws Exception {
        Integer id = station.getId();
        if(id == null) {
            return null;
        } else {
            return stationService.getStationById(id);
        }
    }

    @RequestMapping("/saveOrUpdate")
    public Map<String, Object> saveOrUpdate(@RequestBody Station station) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Integer id = station.getId();
        if (Objects.isNull(id)) {
            stationService.save(station);
        } else {
            stationService.update(station);
        }
        result.put("success", true);
        return result;
    }

    @RequestMapping("/deleted")
    public Map<String, Object> deleted(@RequestBody Station station) throws Exception {
        Map<String, Object> result = new HashMap<>();
        Integer id = station.getId();
        if (Objects.isNull(id)) {
            result.put("msg", "充电桩id不存在");
        } else {
            stationService.deleted(station.getId());
            result.put("success", true);
        }
        return result;
    }
}
